package com.brainardphotography.blog.web;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.brainardphotography.blog.BlogEntry;
import com.brainardphotography.blog.dao.BlogEntryDAO;
import com.brainardphotography.blog.utils.Utils;
import com.google.inject.Inject;
import com.google.sitebricks.At;
import com.google.sitebricks.Show;
import com.google.sitebricks.http.Get;

@At("/rss/2.0.xml")
@Show("Rss20.xml")
public class RssPage {
	private List<BlogEntry> blogEntries;
	private Date lastUpdated;
	private String serverName;

	@Inject
	private HttpServletRequest request;
	
	public String getLastUpdated() {
		
		return Utils.formatDateAsRFC822(lastUpdated);
	}
	
	public String getServerName() {
		return serverName;
	}
	
	public List<BlogEntry> getBlogEntries() {
		return blogEntries;
	}

	@Get()
	public void get20() {
		BlogEntryDAO dao = new BlogEntryDAO();

		blogEntries = dao.findAll();

		if(blogEntries.size() > 0)
			this.lastUpdated = blogEntries.get(0).getCreated();
		else
			this.lastUpdated = new Date();
		
		
		
		this.serverName = new StringBuffer()
				.append("http").append(request.isSecure() ? "s" : "").append("://")
				.append(request.getServerName())
				.append(request.getServerPort() != 80 ? (":" + request.getServerPort()) : "")
				.toString();
	}
}
